package com.es.bst;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * @author 二十
 * @since 2022/1/17 2:22 上午
 */
public class BinarySearchTree<E extends Comparable<E>> extends AbstractBinarySearchTree<E> {

    private class Node {
        private E data;
        private Node left, right;

        public Node(E e) {
            data = e;
        }

        @Override
        public String toString() {
            return "data:" + data;
        }
    }

    private int size;

    private Node root;

    public BinarySearchTree() {

    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public void add(E data) {
        root = add(data, root);
    }

    private Node add(E data, Node node) {
        if (node == null) {
            size++;
            return new Node(data);
        }
        if (node.data.compareTo(data) > 0) {
            node.left = add(data, node.left);
        }
        if (node.data.compareTo(data) < 0) {
            node.right = add(data, node.right);
        }
        return node;
    }

    @Override
    public boolean contains(E e) {
        return contains(e, root);
    }

    private boolean contains(E e, Node node) {
        if (node == null) {
            return false;
        }
        if (node.data == e) {
            return true;
        } else if (node.data.compareTo(e) < 0) {
            return contains(e, node.left);
        } else if (node.data.compareTo(e) > 0) {
            return contains(e, node.right);
        }
        return false;
    }

    @Override
    public void preOrder() {
        preOrder(root);
    }

    private void preOrder(Node node) {
        if (node == null) {
            return;
        }
        System.out.println(node.data);
        preOrder(node.left);

        preOrder(node.right);
    }

    @Override
    public void midOrder() {
        midOrder(root);
    }

    private void midOrder(Node node) {
        if (node == null) {
            return;
        }
        midOrder(node.left);
        System.out.println(node.data);
        midOrder(node.right);
    }

    @Override
    public void postOrder() {
        postOrder(root);
    }

    private void postOrder(Node node) {
        if (node == null) {
            return;
        }
        postOrder(node.left);
        postOrder(node.right);
        System.out.println(node.data);
    }

    @Override
    public void preOrderNR() {
        Stack<Node> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            Node node = stack.pop();
            System.out.println(node.data);
            if (node.right != null) {
                stack.push(node.right);
            }
            if (node.left != null) {
                stack.push(node.left);
            }
        }
    }

    @Override
    public void guangOrder() {
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            Node node = queue.poll();
            System.out.println(node.data);
            if (node.left != null) {
                queue.add(node.left);
            }
            if (node.right != null) {
                queue.add(node.right);
            }
        }
    }

    @Override
    public E minElement() {
        if (size == 0) {
            return null;
        }
        return minElement(root).data;
    }

    private Node minElement(Node node) {
        if (node.left == null) {
            return node;
        }
        return minElement(node.left);
    }

    @Override
    public E removeMinElement() {

        Node node = minElement(root);
        root = removeMinElement(root);
        return node.data;
    }

    private Node removeMinElement(Node node) {
        if (node.left == null) {
            Node r = node.right;
            node.right = null;
            size--;
            return r;
        }
        node.left = removeMinElement(node.left);
        return node;
    }

    @Override
    public void remove(E e) {
        root = remove(root, e);
    }

    private Node remove(Node node, E e) {
        //如果node是空，那就直接返回null
        if (node == null) {
            return null;
        }
        //如果待删除元素落在了node的左子树上，
        if (e.compareTo(node.data) < 0) {
            node.left = remove(node.left, e);
            return node;
        } else if (e.compareTo(node.data) > 0) {
            //如果待删除元素落在了node的右子树上
            node.right = remove(node.right, e);
            return node;
        } else {
            //node节点就是待删除的元素
            if (node.left == null) {
                Node right = node.right;
                node.right = null;
                size--;
                return right;
            }
            if (node.right == null) {
                Node left = node.left;
                node.left = null;
                size--;
                return left;
            }

            //否则就是node的左右节点都有数据的情况
            //这个时候需要找到比node节点大的最小节点，也就是node右子树上的最小节点
            Node min = minElement(node.right);
            //新节点的右节点就指向（移除新节点之后的右子树）
            min.right = removeMinElement(node.right);
            //新节点的左节点指向 原节点的左节点
            min.left = node.left;
            //help GC
            node.left = node.right = null;
            //返回移除指定元素后的新替换节点
            return min;
        }

    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("BinarySearchTree:\n");
        toString(root, 0, sb);
        return sb.toString();
    }

    private void toString(Node node, int depth, StringBuilder sb) {
        if (node == null) {
            return;
        }
        toString(node.left, depth + 1, sb);
        sb.append(toString(depth) + node.data + "\n");
        toString(node.right, depth + 1, sb);
    }

    private String toString(int depth) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < depth; i++) {
            sb.append("--");
        }
        return sb.toString();
    }
}
